#include "messagePickUp.h"
#include "networkessential.h"
#include <memory.h>

#define IP_MAX 20

messagePickUp::messagePickUp()
:m_str_message(NULL)
{}

messagePickUp::messagePickUp(char *str_message)
:m_str_message(str_message)
{}

void messagePickUp::setMessageAddr(char *str_message)
{
	m_str_message = str_message;
}

void messagePickUp::pickUpInfo(std::string &groupname, asgka_element content)
{
	// Message content :
	// groupname_len groupname content_len content
	int i = 0;
	int retval;
	retval = getStringfromChars(m_str_message + i, groupname);
	i += retval;
	retval = getCharContent(m_str_message + i, content);
}

void messagePickUp::pickUpInfo(std::string &groupname,
		asgka_element ciphertext1, asgka_element ciphertext2, asgka_element ciphertext3)
{
	// Message content:
	// groupname_len groupname
	// ciphertext_len ciphertext1 ciphertext2 ciphertext3
	int i = 0;
	int retval;
	retval = getStringfromChars(m_str_message + i, groupname);
	i += retval;
	retval = getCharContent(m_str_message + i, ciphertext1);
	i += retval;
	retval = getCharContent(m_str_message + i, ciphertext2);
	i += retval;
	retval = getCharContent(m_str_message + i, ciphertext3);
}

void messagePickUp::pickUpInfo(std::string &inviter, std::string &groupname)
{
	// Message content :
	// invitername_len invitername groupname_len groupname
	int i = 0;
	int retval;
	retval = getStringfromChars(m_str_message + i, inviter);
	i += retval;
	retval = getStringfromChars(m_str_message + i, groupname);
}

void messagePickUp::pickUpInfo(std::string &groupname,
		std::map<std::string, u_long> &member_list)
{
	// Message content:
	// groupname_len groupname
	// membercount membername_len1 membername1 in_addr ... ...
	int i = 0;
	int retval;
	retval = getStringfromChars(m_str_message + i, groupname);
	i += retval;
	getMapPairfromChars(m_str_message + i, member_list);
}

void messagePickUp::pickUpInfo(std::string &sendername)
{
	// Message content:
	// sendername_len sendername
	getStringfromChars(m_str_message, sendername);
}

int messagePickUp::getStringfromChars(char *beginAddr, std::string &buf)
{
	int i = IntLength;
	int string_len = NetCharToFourBytes<int>(beginAddr);

	char *str_string = new char[string_len + 1];
	memset(str_string, 0, string_len + 1);

	for (int j = 0; j < string_len; j++)
		str_string[j] = beginAddr[i++];

	buf.clear();
	buf.append(str_string);

	delete str_string;
	return i;
}

int messagePickUp::getCharContent(char *beginAddr, char *buf)
{
	int i = IntLength;
	int len = NetCharToFourBytes<int>(beginAddr);

	memcpy(buf, beginAddr + i, len);
	i += len;

	return i;
}


// Changed
int messagePickUp::getMapPairfromChars(char *beginAddr, std::map<std::string,u_long> &member_list)
{
	int i = IntLength;
	int retval;
	int membercount = NetCharToFourBytes<int>(beginAddr);

	for (int j = 0; j < membercount; j++)
	{
		u_long addr;
		std::string member_name;
		retval = getStringfromChars(beginAddr + i, member_name);
		i += retval;
		addr = NetCharToFourBytes<u_long>(beginAddr + i);
		i += IntLength;

		member_list.insert(stouvalType(member_name, addr));
	}
	return i;
}
